---
# Copyright 2017, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Gather address fact
  set_fact:
    # NOTE(cloudnull):
    #  Collect the interface address from hostvars of a target node.
    #  Check if the host is running in a container. If not, pull the bridge data from the
    #  network interface. If an interface bridge is defined, pull the IP address from the
    #  physical network device. If no physical bridge is defined collect the address from
    #  the "<INTERFACE>_network_data" variable. If nothing is defined use the
    #  "ansible_host" address.
    _lookup_address: >-
      {%- set _network_data = hostvars[inventory_hostname]['container_networks'][network_address] | default({}) -%}
      {%- if is_metal is defined and is_metal | bool -%}
      {%-   set _bridge = _network_data['bridge'] | default('no_bridge_defined') | replace('-', '_') -%}
      {%- else -%}
      {%-   set _bridge = 'no_bridge_defined' -%}
      {%- endif -%}
      {%- if _bridge != 'no_bridge_defined' and hostvars[inventory_hostname]['ansible_' + _bridge] is defined and hostvars[inventory_hostname]['ansible_' + _bridge]['ipv4'] is defined-%}
      {{ hostvars[inventory_hostname]['ansible_' + _bridge]['ipv4']['address'] }}
      {%- elif _network_data['address'] is defined -%}
      {{ _network_data['address'] }}
      {%- else -%}
      {{ ansible_host }}
      {%- endif -%}
  tags:
    - common-address
    - always

# NOTE(cloudnull):
#  This task is not in dict formation because it is
#  dynamically loading facts based on the network_address
#  and the _lookup_address.
- name: Set address fact
  set_fact: "{{ network_address }}={{ _lookup_address }}"
  tags:
    - common-address
    - always
